package com.atguigu.educenter.service.impl;

import com.atguigu.commonutils.JwtUtils;
import com.atguigu.commonutils.MD5;
import com.atguigu.educenter.entity.UcenterMember;
import com.atguigu.educenter.entity.vo.RegisterVo;
import com.atguigu.educenter.mapper.UcenterMemberMapper;
import com.atguigu.educenter.service.UcenterMemberService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-08-05
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    private RedisTemplate<String,String> redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate<String, String> redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    @Override
    public String login(UcenterMember ucenterMember) {
        // 获取登录手机号与密码
        String mobile = ucenterMember.getMobile();
        String password = ucenterMember.getPassword();

        // 首先根据手机号获取用户信息
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileMember = baseMapper.selectOne(wrapper);
        if(mobileMember == null){
            throw new GuliException(20001,"手机号不存在!");
        }

        // 判断密码时候正确
        // 数据库中存储的数据为MD5加密过的数据
        // 把输入的密码按照MD5的方式加密,然后在跟数据库比较
        if(!MD5.encrypt(password).equals(mobileMember.getPassword())){
            throw new GuliException(20001,"密码错误!");
        }

        // 判断用户是否禁用
        if(mobileMember.getIsDisabled()){
            throw new GuliException(20001,"该用户已经禁用!");
        }

        // 登录成功,生成token
        String jwtToken = JwtUtils.getJwtToken(mobileMember.getId(),mobileMember.getNickname());

        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        // 判断属性是否有空值
        if(StringUtils.isEmpty(code) || StringUtils.isEmpty(mobile) ||
                StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password)){
            throw new GuliException(20001,"注册失败!");
        }

        // 在redis获取刚刚发送手机号注册的验证码,与传入验证码比较
        String redisCode = redisTemplate.opsForValue().get(registerVo.getMobile());
        if(!redisCode.equals(code)){
            throw new GuliException(20001,"手机验证码错误!");
        }

        // 判断手机号是否有相同的
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        int count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"该手机已经注册过了,注册失败!");
        }

        // 数据添加到数据库中
        UcenterMember ucenterMember = new UcenterMember();
        ucenterMember.setMobile(mobile);
        ucenterMember.setNickname(nickname);
        ucenterMember.setPassword(MD5.encrypt(password)); // 将密码进行MD5加密
        ucenterMember.setIsDisabled(false); // 用户默认不被禁用
        ucenterMember.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132"); // 设置默认头像
        baseMapper.insert(ucenterMember);
    }

    @Override
    public Integer countRegisterByDay(String day) {
        return baseMapper.countRegisterDay(day);
    }
}
